package cn.kduck.webapp.security;

import cn.kduck.assembler.security.authentication.AuthenticationType;
import cn.kduck.assembler.security.authentication.UserNameService;
import cn.kduck.module.account.service.Account;
import cn.kduck.module.account.service.AccountService;
import cn.kduck.module.user.service.User;
import cn.kduck.module.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserNameByPhoneNumService implements UserNameService {

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserService userService;

    @Override
    public String getUserName(AuthenticationType type, Object loginName) {
        User user = null;
//        if(type == AuthenticationType.PHONE_NUM){
            user = userService.getUserByPhone(loginName.toString());
//        }

        if(user == null){
            throw new RuntimeException("用户不存在,type=" + type +",loginName=" + loginName);
        }

        Account account = accountService.getAccountByUserId(user.getUserId());
        if(account == null){
            throw new RuntimeException("用户账户不存在,userId=" + user.getUserId());
        }
        return account.getAccountName();
    }
}
